package cn.zw.frr.ringbuffer;

import cn.zw.frr.service.IEventService;
import cn.zw.frr.factory.DataEvent;
import com.lmax.disruptor.WorkHandler;
import com.sun.javaws.IconUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class EventHandler implements WorkHandler<DataEvent>{


    private Class param;
    private String methodName;
    private Logger logger = LoggerFactory.getLogger(EventHandler.class);

    static AtomicLong atomicLong = new AtomicLong();

    IEventService iEventService;

    static ConcurrentHashMap<Integer,IEventService> concurrentHashMap = new ConcurrentHashMap<>();

    Integer iEventServiceNum;


    public EventHandler(IEventService iEventService) {
        this.iEventService = iEventService;
    }


    public EventHandler(IEventService iEventService, String methodName, Class param) {
        this.iEventService = iEventService;
        this.methodName = methodName;
        this.param = param;
    }

    public EventHandler(IEventService iEventService, String methodName) {
        this.iEventService = iEventService;
        this.methodName = methodName;
    }

    public EventHandler(Integer iBaseServiceNum, ConcurrentHashMap<Integer, IEventService> map, IEventService iEventService, String methodName, Class param) {
        this.iEventServiceNum = iBaseServiceNum;
        concurrentHashMap = map;
        this.methodName = methodName;
        this.param = param;
    }

    public EventHandler(Integer iBaseServiceNum, ConcurrentHashMap<Integer, IEventService> map, IEventService iEventService, String methodName) {
        this.iEventServiceNum = iBaseServiceNum;
        concurrentHashMap = map;
        this.methodName = methodName;
    }

    public IEventService getiEventService() {
        return iEventService;
    }

    public void setiEventService(IEventService iEventService) {
        this.iEventService = iEventService;
    }

    @Override
    public void onEvent(DataEvent event) {
        if(event == null || event.getValue()==null){
            logger.warn("event or event value is null!");
            return;
        }
        if(concurrentHashMap.size()==0){

            if(methodName!=null){
                try {
                    if(param!=null){
                        System.out.println(iEventService.getClass().getDeclaredMethod(methodName,param).invoke(iEventService, event.getValue()));
                    }else{
                        System.out.println(iEventService.getClass().getDeclaredMethod(methodName).invoke(iEventService, null));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println(iEventService.invoke(event.getValue()));
            }

        }else{

            if(methodName!=null){
                try {
                    if(param!=null){
                        System.out.println(concurrentHashMap.get(iEventServiceNum).getClass().getDeclaredMethod(methodName,param).invoke(concurrentHashMap.get(iEventServiceNum), event.getValue()));
                    }else{
                        System.out.println(concurrentHashMap.get(iEventServiceNum).getClass().getDeclaredMethod(methodName).invoke(concurrentHashMap.get(iEventServiceNum), null));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println(concurrentHashMap.get(iEventServiceNum).invoke(event.getValue()));
            }

        }
        logger.info("exec onEvent！");
    }


}
